import React, {useEffect, useState} from 'react'
import {InputNumber, message} from 'antd'
import {editHibernate, getHibernate} from '@/service/system/hibernate'
import styles from './index.less'

const hibernate = () => {
	const [value, setValue] = useState<any>()
	useEffect(() => {
		getHibernate('sleep_time').then((res) => {
			if (res?.success) {
				setValue(res?.data?.value)
			}
		})
	}, [])

	return (
		<div className={styles.hibernate}>
			<div className={styles.title}>用户休眠规则配置</div>
			正常状态未登陆系统超过：
			<InputNumber
				value={value}
				min={0}
				max={99999}
				onBlur={(e) => {
					const pattern = /^([1-9]\d{0,4}|0|\.[1-9]{1})(\.\d{1,2})?$/
					const day = e?.target?.value
					if (Number(day) > 99999) {
						message.warning('不能大于99999')
					} else if (pattern.test(day)) {
						editHibernate({
							key: 'sleep_time',
							value: day,
						}).then((res) => {
							if (res?.success) {
								message.success(res?.msg)
								setValue(day)
							}
						})
					} else {
						message.warning('请输入数字且不能小于等于0')
					}
				}}
			/>{' '}
			天，正常状态改为睡眠状态
		</div>
	)
}

export default hibernate
